<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        canvas {
            border: 1px solid;
        }
    </style>
</head>

<body>
    <canvas width="500" height="500" id="mycanvas"></canvas>

    <script>
        // let arr = [
        //     "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2Ftp02%2F1Z9191923035R0-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1661397561&t=a620034e48d31890e7e652561c79083b",
        //     "https://img2.baidu.com/it/u=1713796643,4188877777&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500",
        //     "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2Ftp05%2F1Z9291TIBZ6-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1661397561&t=d113fcaf5515f719281a37f6e928b213"
        // ];

        //预加载:  不能控制加载的顺序
        // function loadImg(arr, cb) {
        //     let count = 0;
        //     let imgarr = [];
        //     arr.forEach(src => {
        //         let imgEl = new Image();
        //         imgEl.src = src;
        //         imgEl.onload = function () {
        //             count++;
        //             imgarr.push(this);
        //             if (count >= arr.length) {
        //                 //加载好了
        //                 cb(imgarr);
        //             }
        //         }
        //     })
        // }



        let obj = {
            bg1: "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2Ftp02%2F1Z9191923035R0-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1661397561&t=a620034e48d31890e7e652561c79083b",
            bg2: "https://img2.baidu.com/it/u=1713796643,4188877777&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500",
            bg3: "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2Ftp05%2F1Z9291TIBZ6-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1661397561&t=d113fcaf5515f719281a37f6e928b213"
        };

        //控制  可以具体加载哪一张
        function loadImg(obj, cb) {
            return new Promise((resolve) => {
                //知道  对象的长度
                let length = 0;
                for (let key in obj) {
                    length++;
                }
                console.log("对象的长度", length);
                //记录图片加载好的数量
                let count = 0;
                //循环加载对象里的图片
                let resObj = {}; //一一对应的关系
                for (let k in obj) {
                    let imgEl = new Image();
                    imgEl.src = obj[k];
                    imgEl.onload = function () {
                        count++;
                        resObj[k] = this;
                        if (count >= length) {
                            // 所有的图片加载完毕
                            cb && cb(resObj);
                            resolve(resObj);
                        }
                    }
                }
            })
        }





        let canvas = document.querySelector("#mycanvas");
        //获取上下文 :  画笔
        let context = canvas.getContext("2d");
        // loadImg(arr, (imgarr) => {
        //     context.drawImage(imgarr[0], 0, 0, 100, 100);
        //     context.drawImage(imgarr[1], 200, 200, 100, 100);
        //     context.drawImage(imgarr[2], 300, 300, 100, 100);

        // })
        loadImg(obj).then(resObj=>{
            console.log(resObj);
            context.drawImage(resObj['bg1'], 0, 0, 100, 100);
            context.drawImage(resObj['bg2'], 200, 200, 100, 100);
            context.drawImage(resObj['bg3'], 300, 300, 100, 100)
        })
    </script>
</body>

</html>